/**
 *
 */
package util;

import com.dangdi.controller.myRetryHandler;
import com.sun.deploy.net.HttpResponse;
import org.apache.http.Header;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerPNames;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultHttpRoutePlanner;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.*;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @��Ŀ DangD
 * @���� User
 * @ʱ�� 2018��6��22�� ����10:09:23
 * @���� HttpClientUtil
 * @·��
 */
public class HttpClientUtil2 {
    public org.apache.http.HttpResponse HttoPost(Map<String, Object> params, String url) throws URISyntaxException, IOException, InterruptedException {
        DefaultHttpClient client = new DefaultHttpClient();
        // ʵ����HTTP����
        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
        client.setHttpRequestRetryHandler(new myRetryHandler());
        HttpPost request = new HttpPost();
        request.setURI(new URI(url));
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        List<String> tagList = (List<String>) params.get("data_rfid");
        if (params != null) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
            }
        }
        request.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
        org.apache.http.HttpResponse response = client.execute(request);
        return response;
    }

    public org.apache.http.HttpResponse httpGet(String url) throws IOException, InterruptedException {

        DefaultHttpClient client =new DefaultHttpClient();
        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
        client.setHttpRequestRetryHandler(new myRetryHandler());
        HttpGet httpGet = new HttpGet(url);
        org.apache.http.HttpResponse response;
        response=  client.execute(httpGet);
        return response;
    }


    private DefaultHttpClient getHttpClient()
    {
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("https", SSLSocketFactory
                .getSocketFactory(), 443));

        HttpParams params = new BasicHttpParams();
        int timeoutConnection = 30000;
        HttpConnectionParams.setConnectionTimeout(params, timeoutConnection);
        int timeoutSocket = 30000;
        HttpConnectionParams.setSoTimeout(params, timeoutSocket);

        params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
        params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE,
                new ConnPerRouteBean());
        params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(
                params, schemeRegistry);

        SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
        DefaultHttpClient client = new DefaultHttpClient(cm, params);
        client.setRoutePlanner(new DefaultHttpRoutePlanner(schemeRegistry));
        return (client);
    }


}
